/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package share;

import org.nustaq.serialization.simpleapi.DefaultCoder;

/**
 *
 * @author Salm
 */
public class ByteArraySerializer {
    private final ThreadLocal<DefaultCoder> tlCoders = new ThreadLocal();
    
    public byte[] serialize(Object o)
    {
        return getCoder().toByteArray(o);
    }
    
    public <E> E deserialize(byte[] b, Class<E> c)
    {
        return c.cast(getCoder().toObject(b));
    }
    
    private DefaultCoder getCoder()
    {
        if (tlCoders.get() == null)
        {
            tlCoders.set(makeCoder());
        }
        
        return tlCoders.get();
    }
    
    private DefaultCoder makeCoder()
    {
        return new DefaultCoder();
    }
}
